package bb.lanxing.activity;

import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.databinding.ActivityPowerSectionSettingBinding;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.mvvm.sportsetting.SportSettingViewModel;
import bb.lanxing.mvvm.sportsetting.UserSportSetting;
import bb.lanxing.util.Log;
import bb.lanxing.util.ui.InputHelper;
import bb.lanxing.view.BiciAlertDialogBuilder;
import org.apache.commons.cli.HelpFormatter;

public class PowerSectionSettingActivity extends BaseActivity {
    final private static String TAG = "PowerSectionSettingActivity";
    ActivityPowerSectionSettingBinding sectionSettingBinding;
    private int[] sections = {0, 0, 0, 0, 0, 0, 0, 0};
    private double[] powerPercent = {Utils.DOUBLE_EPSILON, 0.56d, 0.75d, 0.91d, 1.06d, 1.21d, 1.5d};
    SportSettingViewModel viewModel = new SportSettingViewModel(App.getContext());

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        sectionSettingBinding = DataBindingUtil.setContentView(this, R.layout.activity_power_section_setting);
        setupActionBar(true);
        initData();
        initView();
    }

    private void initView() {
        sectionSettingBinding.learnPowerZone.setOnClickListener(v -> showZoneInfo());
        updateSectionView();
    }

    public void updateSectionView() {
        String valueOf;
        String valueOf2;
//        int[] iArr = {166, 167, 168, 169, 170, 171, 172};
        int length = sections.length - 1;
        while (length > 0) {
            int[] iArr2 = sections;
            int i = iArr2[length];
            if (length == iArr2.length - 1) {
                valueOf2 = String.valueOf(i);
                valueOf = "Max";
            } else {
                valueOf = String.valueOf(iArr2[length + 1]);
                valueOf2 = String.valueOf(sections[length]);
            }
            String string = getString(length == sections.length - 1 ? R.string.mine_section_setting_power_value_max : R.string.mine_section_setting_power_value, valueOf, valueOf2);
            int indexOf = string.indexOf(HelpFormatter.DEFAULT_OPT_PREFIX);
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.grey_999999));
            ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.grey_666666));
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
            spannableStringBuilder.setSpan(foregroundColorSpan, 0, indexOf, 33);
            spannableStringBuilder.setSpan(foregroundColorSpan2, indexOf + 1, string.length(), 33);
//            sectionSettingBinding.setVariable(iArr[length - 1], spannableStringBuilder);
            switch (length) {
                case 1 -> sectionSettingBinding.powerZ1.setText(spannableStringBuilder);
                case 2 -> sectionSettingBinding.powerZ2.setText(spannableStringBuilder);
                case 3 -> sectionSettingBinding.powerZ3.setText(spannableStringBuilder);
                case 4 -> sectionSettingBinding.powerZ4.setText(spannableStringBuilder);
                case 5 -> sectionSettingBinding.powerZ5.setText(spannableStringBuilder);
                case 6 -> sectionSettingBinding.powerZ6.setText(spannableStringBuilder);
                case 7 -> sectionSettingBinding.powerZ7.setText(spannableStringBuilder);
                default -> {}
            }

            length--;
        }
    }

    private void initData() {
        int powerMax = RemoteSharedPreference.getInstance().getPowerMax();
        TextView textView = sectionSettingBinding.warningValueView;
        textView.setText(String.valueOf(powerMax));

        sectionSettingBinding.warningValueLayout.setOnClickListener(view -> {
            showEditDialog(sectionSettingBinding.warningValueView.getText().toString());
        });
        updateSection(powerMax);
    }

    public void showEditDialog(final String str) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.edittext_view, null);
        final EditText editText = inflate.findViewById(R.id.editText);
        editText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS | InputType.TYPE_CLASS_NUMBER);
        editText.setMaxLines(1);
        if (TextUtils.isEmpty(str)) {
            editText.setHint(R.string.dialog_hint_input);
        } else {
            editText.setText(str);
        }

        AlertDialog.Builder mNegativeButton =
                new BiciAlertDialogBuilder(this)
                        .setView(inflate)
                        .setPositiveButton(R.string.dialog_btn_ok, (dialogInterface, i) -> {
                            String obj = editText.getText().toString();
                            if (TextUtils.isEmpty(obj) || obj.equals(str)) {
                                dialogInterface.cancel();
                            } else {
                                int abs = Math.abs(Integer.parseInt(obj));
                                updateSection(abs);
                                RemoteSharedPreference.getInstance().setPowerMax(abs);
                                SharedManager.getInstance().setPowerSectionWithMP(makeSectionString(sections));
                                updateSectionView();
                                sectionSettingBinding.warningValueView.setText(obj);
                                updateUserFtp("ftp", abs);
                            }
                            InputHelper.hideSoftInput(editText);
                        })
                        .setNegativeButton(R.string.dialog_btn_cancel, (dialogInterface, i) -> InputHelper.hideSoftInput(editText));
        mNegativeButton.setTitle(R.string.mine_section_setting_dialog_input);
        mNegativeButton.show();
        InputHelper.showSoftInput(editText);
    }

    public void updateSection(int i) {
        if (sections == null) {
            return;
        }
        int i2 = 0;
        while (true) {
            int[] iArr = sections;
            if (i2 >= iArr.length) {
                return;
            }
            if (i2 == 0) {
                iArr[i2] = (int) (i * powerPercent[i2]);
            } else {
                iArr[i2] = (int) (i * powerPercent[i2 - 1]);
            }
            i2++;
        }
    }

    public String makeSectionString(int[] iArr) {
        String str = "";
        for (int i = 1; i < iArr.length; i++) {
            str = str + iArr[i];
            if (i < iArr.length - 1) {
                str = str + ";";
            }
        }
        Log.v(TAG, "makeSectionString, jsonStr = " + str);
        return str;
    }

    public void updateUserFtp(String str, int i) {
        viewModel.updateSportSetting(App.getContext().getUserId(), new UserSportSetting(null, null, null, i, null, null));
    }
    public void showZoneInfo() {
        Spanned fromHtml;
        if (Build.VERSION.SDK_INT >= 24) {
            fromHtml = Html.fromHtml(getString(R.string.mine_section_setting_power_about_zone), 0);
        } else {
            fromHtml = Html.fromHtml(getString(R.string.mine_section_setting_power_about_zone));
        }
        WindowManager windowManager = getWindowManager();
        new BiciAlertDialogBuilder(PowerSectionSettingActivity.this)
                .size((int) (windowManager.getDefaultDisplay().getWidth() * 0.9d), (int) (windowManager.getDefaultDisplay().getHeight() * 0.8d))
                .setMessage(fromHtml).show();
    }

}
